package com.huarui.dictionary.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.ewaf.framework.action.BaseAction;

import com.huarui.dictionary.DictionaryEnum;
import com.huarui.dictionary.model.Dictionary;
import com.huarui.dictionary.service.DictionaryReadOnlyService;
import com.huarui.dictionary.service.DictionaryService;
import com.huarui.params.Params;

public class DictionaryAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3006891581549359816L;

	private DictionaryService dictionaryService;
	
	 /**
     * 字典只读service
     */
    private DictionaryReadOnlyService dictionaryReadOnlyService;

	private Dictionary dictionary;

	private DictionaryEnum[] groupList;

	private List<Dictionary> subDictionaryList;
	
	private Map<String, Object> jsonObject;
	
	private Map<String, Object> mesMap;

	private String group;
	
	private String queryParam;

	private Integer start;
	
	public Integer getStart() {
		return start;
	}


	public void setStart(Integer start) {
		this.start = start;
	}


	public Map<String, Object> getMesMap() {
		return mesMap;
	}


	public void setMesMap(Map<String, Object> mesMap) {
		this.mesMap = mesMap;
	}



	public String getQueryParam() {
		return queryParam;
	}


	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}


	public String toAdd() {
		groupList = dictionaryService.getGroupValues();
		return INPUT;
	}

	
	public String doAdd() {

		dictionaryService.createDictionary(dictionary);
		return SUCCESS;
	}

	public String toList() {

		groupList = dictionaryService.getGroupValues();
		if (group == null) {
			return "toList";
		}
		subDictionaryList = dictionaryService
				.getSubDictionaryListByGroup(group);

		return "toList";
	}

	/**
	 * 根据组别相应的组记录
	 * 
	 * @return
	 */
	public String findByVarGroup() {

		subDictionaryList = dictionaryReadOnlyService.getDictionaryByGroup(group);

		return JSON;
	}
	
	public String findByVarGroupRoot() {
		jsonObject = new HashMap<String, Object>();
		
		
		subDictionaryList = dictionaryReadOnlyService.getDictionaryByGroup(group);
		jsonObject.put("root", subDictionaryList);
		return JSON;
	}

	public String toModi() {

		return "modi";
	}

	public String doModi() {

		return SUCCESS;
	}

	public Dictionary getDictionary() {
		return dictionary;
	}

	public DictionaryEnum[] getGroupList() {
		return groupList;
	}

	public void setGroupList(DictionaryEnum[] groupList) {
		this.groupList = groupList;
	}

	public void setDictionary(Dictionary dictionary) {
		this.dictionary = dictionary;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public List<Dictionary> getSubDictionaryList() {
		return subDictionaryList;
	}

	public void setSubDictionaryList(List<Dictionary> subDictionaryList) {
		this.subDictionaryList = subDictionaryList;
	}

	public void setDictionaryReadOnlyService(
			DictionaryReadOnlyService dictionaryReadOnlyService) {
		this.dictionaryReadOnlyService = dictionaryReadOnlyService;
	}


	public Map<String, Object> getJsonObject() {
		return jsonObject;
	}


	/**
	 * 查询字典数据列表(后台查询)
	 * @author ycf
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listDictionary() {
		Map<String, Object> paramsMap = null;
		if(queryParam == null) {
			paramsMap = new HashMap<String, Object>();
		} else {
			JSONObject jo = JSONObject.fromObject(queryParam);
			paramsMap = (Map<String, Object>) JSONObject.toBean(jo, HashMap.class);
		}
		paramsMap.put("start", start == null ? 0 : start);
		paramsMap.put("pageSize", Params.BACKGROUND_PAGE_SIZE);
		mesMap = dictionaryReadOnlyService.listDictionaryByComplex(paramsMap);
		return SUCCESS;
	}

}
